package com.tonyodev.fetch2core;

import com.tonyodev.fetch2core.server.FetchFileResourceTransporter;
import com.tonyodev.fetch2core.server.FileRequest;

import org.jetbrains.annotations.NotNull;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;

/**
 * 文件服务下载接口
 *
 * @since 2021-05-20
 */
public interface FileServerDownloader extends Downloader<FetchFileResourceTransporter, FileServerDownloader.TransporterRequest> {
    /**
     * Fetch a Catalog List of File Resources from a Fetch File Server
     *
     * @param serverRequest the server request
     * @return list of File Resources
     * @throws Exception Exception
     */
    @NotNull
    List<FileResource> getFetchFileServerCatalog(@NotNull ServerRequest serverRequest) throws Exception;

    /**
     * Class used to hold configuration settings for a FetchFileServer Transporter connection.
     *
     * @since 2021-06-16
     */
    static class TransporterRequest {
        private InetSocketAddress inetSocketAddress = new InetSocketAddress(0);

        private FileRequest fileRequest = new FileRequest();

        /**
         * 获取socket链接地址
         *
         * @return InetSocketAddress
         */
        public InetSocketAddress getInetSocketAddress() {
            return this.inetSocketAddress;
        }

        /**
         * 设置地址
         *
         * @param inetSocketAddress socket链接地址
         */
        public void setInetSocketAddress(@NotNull InetSocketAddress inetSocketAddress) {
            this.inetSocketAddress = inetSocketAddress;
        }

        /**
         * 获取请求
         *
         * @return 请求
         */
        public FileRequest getFileRequest() {
            return this.fileRequest;
        }

        /**
         * 设置请求
         *
         * @param fileRequest 请求
         */
        public void setFileRequest(@NotNull FileRequest fileRequest) {
            this.fileRequest = fileRequest;
        }

        /**
         * 对比
         *
         * @param other 其他对象
         * @return 对比 boolean
         */
        @Override
        public boolean equals(Object other) {
            if (this == other) {
                return true;
            }
            if (other == null || getClass() != other.getClass()) {
                return false;
            }
            TransporterRequest that = (TransporterRequest) other;
            return Objects.equals(inetSocketAddress, that.inetSocketAddress)
                && Objects.equals(fileRequest, that.fileRequest);
        }

        /**
         * 获取hashCode
         *
         * @return hashCode
         */
        @Override
        public int hashCode() {
            int result = this.inetSocketAddress.hashCode();
            result = 31 * result + this.fileRequest.hashCode();
            return result;
        }

        /**
         * 输出string
         *
         * @return toString
         */
        @Override
        public String toString() {
            return "TransporterRequest("
                + "inetSocketAddress=" + inetSocketAddress
                + ", fileRequest=" + fileRequest
                + ')';
        }
    }
}
